home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / eplain / test / pcol.tex < prev    next >
Text File  |  1992-03-02  |  5KB  |  182 lines

  1. % These are the double-column macros from Paul Abrahams, Dec 1991
  2. % They add the following features:
  3. %    \ifbalance  (by default true, causes last page to be balanced)
  4. %    \everycolumn (a token list, useful for marks)
  5. %    \pageeject
  6. %    \eject does a column eject
  7.  
  8. \newtoks\mainoutput
  9. \newbox\partialpage
  10. \newdimen\fullvsize \newdimen \fullhsize
  11. \newbox\leftcolumn  \newbox \rightcolumn
  12. \newbox\savefootins \newskip\footskip 
  13. \newbox\savetopins  \newskip\topinsskip
  14. \newif\ifbalance \balancetrue
  15. \newtoks\everycolumn
  16.  
  17.  
  18. \def\pageeject{\penalty -10005 }%
  19.  
  20. \def\doublecolumns{%
  21.   \mainoutput = \output % Save the previous \output.
  22.   %
  23.   % This grabs any single-column material.
  24.   \output = {\global\setbox\partialpage = \vbox{\unvbox255}}%
  25.   \vskip\abovedoublecolumnskip
  26.   \par % \par updates \pagetotal
  27.   \pagegoal = \pagetotal
  28.   \break % Now expand the \output we assigned just above.
  29.   \output = {\doublecolumnoutput}%
  30.   %
  31.   % Set up \hsize and \vsize for double columns.
  32.   \fullvsize = \vsize
  33.   \fullhsize = \hsize
  34.   \advance\hsize by -\gutter
  35.   \divide \hsize by 2
  36.   \advance\vsize by -\ht\partialpage
  37.   %
  38.   % Take account of existing insertions.
  39.   \ifvoid\footins\else
  40.     \advance\vsize by -\ht\footins
  41.     \advance\vsize by -\skip\footins
  42.   \fi
  43.   \setbox\savefootins = \box\footins
  44.   \footskip = \skip\footins
  45.   %
  46.   \ifvoid\topins\else
  47.     \advance\vsize by -\ht\topins
  48.     \advance\vsize by -\skip\topins
  49.   \fi
  50.   \setbox\savetopins = \box\topins
  51.   \topinsskip = \skip\topins
  52.   %
  53.   % Start at the left, clear the existing columns.
  54.   \let\lr = L%
  55.   \setbox0 = \box\leftcolumn \setbox0 = \box\rightcolumn
  56. }%
  57. %
  58. % This routine actually does the outputting.
  59. \def\doublecolumnoutput{%
  60.    \ifbalance\else \ifnum \outputpenalty < -10000
  61.       \setbox255 = \vbox{\unvbox255 \vfil}%
  62.    \fi\fi
  63.    %
  64.    \the\everycolumn
  65.    \if \lr L%
  66.       \buildcolumn\leftcolumn
  67.       \global\let\lr = R%
  68.    \else
  69.       \buildcolumn\rightcolumn
  70.       \global\let\lr = L%
  71.    \fi
  72.    %
  73.    \ifnum \outputpenalty < -10000
  74.       \ifbalance
  75.       \ifdim \ht\leftcolumn > 8\baselineskip
  76.          \balancecolumns
  77.          \global \let \lr = L%
  78.    \fi\fi\fi
  79.    %
  80.    \if \lr L%
  81.       \setbox255 = \vbox{%
  82.          \unvbox\partialpage
  83.          \dimen0 = \ht\leftcolumn \dimen1 = \dp\leftcolumn
  84.          \joincolumns
  85.          \ifnum \outputpenalty = -10005 \vfil \fi
  86.          }%
  87.       % The restoration of the insertion boxes happens only once, since
  88.       % they are empty thereafter.
  89.       \setbox\footins = \box\savefootins
  90.       \setbox\topins = \box\savetopins
  91.       \vsize = \fullvsize % Because we've taken care of \partialoutput
  92.       \ifnum \outputpenalty = -10006
  93.          \unvbox255
  94.       \else
  95.          \the\mainoutput
  96.          \global\vsize = \fullvsize
  97.       \fi
  98.    \fi
  99. }%
  100. %
  101. % \joincolumns joins \leftcolumn and \rightcolumn into a vbox that aligns
  102. % the tops of the two columns and has the depth of the deeper column.
  103. % It adds the resulting box to the main vertical list.
  104. %
  105. \def\joincolumns{%
  106.    \dimen0 = \dp\leftcolumn \dimen1 = \dp \rightcolumn
  107.    \setbox0 = \hbox to \fullhsize{%
  108.       \vtop to \ht\leftcolumn{\unvbox\leftcolumn}%
  109.       \hfil
  110.       \vtop to \ht\rightcolumn{\unvbox\rightcolumn}%
  111.       }%
  112.    \dimen2 = \boxmaxdepth  % For restoring it later    
  113.    \ifdim \dimen0 < \dimen1
  114.       \boxmaxdepth = \dimen1
  115.    \else
  116.       \boxmaxdepth = \dimen0
  117.    \fi
  118.    \setbox0 = \vbox{\box0}%
  119.    \boxmaxdepth = \dimen2
  120.    \box0  % Produce for the calling context
  121. }%
  122.  
  123. \def\buildcolumn #1{%
  124.    \ifnum \outputpenalty < -9999
  125.       \global \setbox #1 = \vbox{\pagecontents}%
  126.    \else
  127.       \global \setbox #1 = \vbox to \vsize{\pagecontents}%
  128.    \fi
  129. }%
  130. %
  131. % Go back to single-column typesetting.  Unfortunately, we cannot start
  132. % a group in \doublecolumns and end it here, because some documents
  133. % might want double-column mode to continue to the end.  So we have to
  134. % restore the things we've changed manually.
  135. \def\singlecolumn{%
  136.   \penalty -10006
  137.   \output = \mainoutput
  138.   \hsize = \fullhsize
  139.   \vsize = \fullvsize
  140.   %
  141.   % Add \belowdoublecolumnskip if there's enough room, otherwise a \vfil
  142.   % to fill out the page.
  143.   \dimen0 = \pagetotal \advance \dimen0 by \belowdoublecolumnskip
  144.   \ifdim \dimen0 < \pagegoal
  145.     \vskip\belowdoublecolumnskip
  146.   \else
  147.     \vfil
  148.   \fi
  149.   \allowbreak
  150. }%
  151. %
  152. \def \balancecolumns{%
  153.    \setbox\leftcolumn = \vbox{\unvbox\leftcolumn \unskip}%
  154.    \setbox\rightcolumn = \vbox{\unvbox\rightcolumn \unskip}%
  155.    \setbox0 = \vbox{%
  156.       \dimen0 = \dp\leftcolumn
  157.       \unvbox \leftcolumn
  158.       \if \lr L
  159.          \kern -\dimen0
  160.          \vskip -\topskip      
  161.          \vskip \baselineskip
  162.          \unvbox \rightcolumn
  163.       \fi
  164.       }%
  165.    \dimen0 = \ht0
  166.    \advance \dimen0 by \topskip \advance \dimen0 by -\baselineskip
  167.    \divide \dimen0 by 2 \splittopskip = \topskip
  168.    \count@ = \vbadness \vbadness = 10000
  169.    \loop
  170.       \setbox2 = \copy0
  171.       \setbox1 = \vsplit2 to \dimen0
  172.    \ifdim \ht2 > \dimen0
  173.       \advance \dimen0 by 1pt
  174.    \repeat
  175.    \vbadness = \count@
  176.    \setbox \leftcolumn = \vbox to \dimen0{\unvbox1}%
  177.    \setbox \rightcolumn = \vbox to \dimen0{\unvbox2}%
  178. }%
  179.